package one.jieyun.ajzw;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;

import java.util.ArrayList;

import one.jieyun.ajzw.adapter.MyFragmentPagerAdapter;

public class MainActivity extends BaseActivity {

    private ViewPager mVpHome;
    private BottomNavigationBar mBottomNavigationBar;
    private ArrayList<Fragment> mFragmentList = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    private void initView(){
        mVpHome = (ViewPager) findViewById(R.id.vp_home);
        mBottomNavigationBar = (BottomNavigationBar) findViewById(R.id.bottom_navigation_bar);

        mBottomNavigationBar.setActiveColor(R.color.colorPrimary)
                .setInActiveColor("#FFFFFF")
                .setBarBackgroundColor("#ECECEC");

        mBottomNavigationBar.setMode(BottomNavigationBar.MODE_SHIFTING).setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_RIPPLE);
        mBottomNavigationBar.addItem(new BottomNavigationItem(R.drawable.ic_home_black_48dp, "首页"))
                .addItem(new BottomNavigationItem(R.drawable.ic_account_balance_black_48dp, "办事部门"))
                .addItem(new BottomNavigationItem(R.drawable.ic_perm_phone_msg_black_48dp, "联系我们"))
                .initialise();

        mBottomNavigationBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int position) {
                Log.i("setTabSelectedListener","onTabSelected"+ position);
                mVpHome.setCurrentItem(position);
            }

            @Override
            public void onTabUnselected(int position) {
                Log.i("setTabSelectedListener","onTabUnselected"+ position);
            }

            @Override
            public void onTabReselected(int position) {
                Log.i("setTabSelectedListener","onTabReselected"+ position);
            }
        });

        mFragmentList.add(new Fragment_Home());
        mFragmentList.add(new Fragment1());
        mFragmentList.add(new Fragment2());

        mVpHome.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                Log.i("addOnPageChangeListener","onPageScrolled");
            }

            @Override
            public void onPageSelected(int position) {
                mBottomNavigationBar.selectTab(position);
                Log.i("addOnPageChangeListener","onPageSelected");

            }

            @Override
            public void onPageScrollStateChanged(int state) {
                Log.i("addOnPageChangeListener","onPageScrollStateChanged");
            }
        });

        MyFragmentPagerAdapter myFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(),mFragmentList);
        mVpHome.setAdapter(myFragmentPagerAdapter);
    }
}
